package com.example.studentknowledgesys.controller;

import com.example.studentknowledgesys.entity.PointRule;
import com.example.studentknowledgesys.service.PointService;
import com.example.studentknowledgesys.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/point-rules")
public class PointRuleController {
    @Autowired
    private PointService pointService;


    // 创建积分规则
    @PostMapping("/add")
    public Result createPointRule(@RequestBody PointRule pointRule) {
        PointRule createdRule = pointService.createPointRule(pointRule);
        return Result.success(createdRule);
    }

    // 更新积分规则
    @PutMapping("/{id}")
    public Result updatePointRule(@PathVariable Integer id, @RequestBody PointRule pointRule) {
        pointRule.setId(id);
        PointRule updatedRule = pointService.updatePointRule(pointRule);
        return Result.success(updatedRule);
    }

    // 删除积分规则
    @DeleteMapping("/{id}")
    public Result deletePointRule(@PathVariable Integer id) {
        boolean deleted = pointService.deletePointRule(id);
        if (deleted) {
            return Result.success();
        }
        return Result.error(400, "删除失败！");
    }

    // 获取所有积分规则
    @GetMapping
    public Result getAllPointRules() {
        List<PointRule> rules = pointService.getAllPointRules();
        return Result.success(rules);
    }

    // 获取启用状态的积分规则
    @GetMapping("/active")
    public Result getActivePointRules() {
        List<PointRule> rules = pointService.getActivePointRules();
        return Result.success(rules);
    }
}
